package com.cloudcc.mobile.view.web;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.MenuToggleEvent;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.view.activity.BindActivity;
import com.litesuits.android.log.Log;

public class Webview extends Fragment {
	private View view;
	private WebView webview;
	private String uri = UrlManager.APP_UR;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// view = View.inflate(getActivity(), R.layout.detail_fragment, null);
		// initWebView(view);
		// /wx_taskquery.action?m=list&objid=task&taskcondition=today

		uri = uri
				+ "/wx_taskquery.action?m=list&objid=task&taskcondition=today";

		return inflater.inflate(R.layout.detail_fragment, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setView();
		setListener();
		super.onActivityCreated(savedInstanceState);
	}

	private void setView() {
		// TODO Auto-generated method stub
		webview = (WebView) getView().findViewById(R.id.web_shenpi);

	}

	private void setListener() {
		Log.d("webview", "webview" + uri);
		// TODO Auto-generated method stub
		// webview.loadUrl("http://www.sogou.com");
		webview.loadUrl(uri);
		// webview.setWebViewClient(new WebViewClient() {
		// public boolean shouldOverrideUrlLoading(WebView view, String url) {
		// Log.d("webview", "webview"+url);
		// if (UrlManager.isWapLogin(url)) {// 跳转去登录
		// EventEngine.register(DetailFragment.this);
		// Intent intent = new Intent(getActivity(), BindActivity.class);
		// startActivity(intent);
		// return true;
		// }else{
		// view.loadUrl(url);
		// return true;
		// }
		//
		// }
		// });
		webview.reload();
		WebSettings webstting = webview.getSettings();
		webstting.setJavaScriptEnabled(true);
		webstting.setAllowFileAccess(true);
		webstting.setSupportZoom(true);
		webstting.setJavaScriptCanOpenWindowsAutomatically(true);
		webstting.setCacheMode(WebSettings.LOAD_NO_CACHE);// 是否缓存
		webview.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				if (url.equals(AppContext.indexUrl)) {
					EventEngine.post(new MenuToggleEvent(false, false));
					webview.stopLoading();
					return;
				}
				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onPageFinished(WebView view, String url) {

				super.onPageFinished(view, url);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {

				Log.d("webview", "webfragment+跳转：" + url);
				if (UrlManager.isWapLogin(url)) {// 跳转去登录
					EventEngine.register(Webview.this);
					Intent intent = new Intent(getActivity(),
							BindActivity.class);
					getActivity().startActivity(intent);
					return true;
				} else {
					// 自身加载新链接,不做外部跳转
					view.loadUrl(url);
					return true;
				}
			}

			public void onReceivedSslError(WebView view,
					android.webkit.SslErrorHandler handler,
					android.net.http.SslError error) {
				handler.proceed();
			};

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				AppContext.isError = true;
				return;
			}
		});
	}

}
